package com.dl7.mvp.music.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.base.BaseFragment;
import com.dl7.mvp.base.baseadapter.BaseRecyclerViewAdapter;
import com.dl7.mvp.music.adapter.MusicAlbumBannerAdapter;
import com.dl7.mvp.music.bean.MusicInfo;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.mvp.databinding.BaseRecyclerviewVerticalBinding;
import com.dl7.mvp.mvp.databinding.HeaderRcmdAlbumsBinding;
import com.dl7.mvp.video.adapter.VideoRcmdListAdapter;
import com.dl7.mvp.video.bean.VideoInfo;
import com.dl7.mvp.video.ui.VideoPlayerActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class RcmdMusicFragment extends BaseFragment<BaseRecyclerviewVerticalBinding> {
    private HeaderRcmdAlbumsBinding mAlbumsHeaderBinding;
    private boolean mHasAddHeader;
    private ArrayList<VideoInfo> mVideoList;

    public RcmdMusicFragment() {
        // Required empty public constructor
    }

    @Override
    public int getContentViewResID() {
        return R.layout.base_recyclerview_vertical;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyclerView();
        loadData();
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        loadData();
    }

    private void initRecyclerView() {
        mAlbumsHeaderBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.header_rcmd_albums, null, false);
        mAlbumsHeaderBinding.rcmdAblumsRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mAlbumsHeaderBinding.getRoot().setLayoutParams(params);

        bindingView.recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    }

    class MyOnItemClickListener implements BaseRecyclerViewAdapter.OnItemClickListener<VideoInfo> {
        @Override
        public void onItemClick(int position, VideoInfo data) {
            VideoPlayerActivity.launch(getContext(), data);
        }
    }

    class OnRcmdAlbumClickListener implements BaseRecyclerViewAdapter.OnItemClickListener<VideoInfo> {
        @Override
        public void onItemClick(int position, VideoInfo data) {
            // VideoAlbumDetailActivity.launch(getContext(), data.getAlbumId(), data.getName());
        }
    }

    private void loadData() {
        RetrofitService.getRcmdMusicAlbumListBanner()
                .compose(bindToLife())
                .subscribe(musicInfos -> {
                    MusicAlbumBannerAdapter adapter = new MusicAlbumBannerAdapter();
                    adapter.addDatas((ArrayList<MusicInfo>) musicInfos);
                    mAlbumsHeaderBinding.rcmdAblumsRecyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    adapter.setOnItemClickListener(new OnRcmdAlbumClickListener());
                    addRcmdAlbumsHeader();
                });
        //
        RetrofitService.getRcmdVideoList(0, 10)
                .compose(bindToLife())
                .subscribe(new Observer<List<VideoInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading();
                    }

                    @Override
                    public void onNext(List<VideoInfo> value) {
                        hideLoading();
                        VideoRcmdListAdapter adapter = new VideoRcmdListAdapter();
                        mVideoList = (ArrayList<VideoInfo>) value;
                        adapter.addDatas(mVideoList);
                        bindingView.recyclerView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                        adapter.setOnItemClickListener(new MyOnItemClickListener());
                        addRcmdAlbumsHeader();
                    }

                    @Override
                    public void onError(Throwable e) {
                        showError();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void addRcmdAlbumsHeader() {
        if (mHasAddHeader) {
            return;
        }
        RecyclerView.Adapter adapter = bindingView.recyclerView.getAdapter();
        if (adapter != null && adapter instanceof BaseRecyclerViewAdapter) {
            ((BaseRecyclerViewAdapter) adapter).addHeader(mAlbumsHeaderBinding.getRoot());
            mHasAddHeader = true;
        }
    }
}
